import 'package:cbec_app/components/base/base_chekbox.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

//预付卡抵扣弹窗

class PrepaymentCardPopup extends StatefulWidget {
  ///预付卡信息
  final Map<String, dynamic> info;

  ///回调预付卡抵现金额
  final Function(String discountAmount)? onConfirm;

  const PrepaymentCardPopup({super.key, required this.info, this.onConfirm});
  @override
  State<PrepaymentCardPopup> createState() => _PrepaymentCardPopupState();
}

class _PrepaymentCardPopupState extends State<PrepaymentCardPopup> {
  int _selected = 1; //当前选中

  @override
  Widget build(BuildContext context) {
    return Container(
        width: 375.w,
        height: 164.w + MediaQuery.of(context).padding.bottom,
        decoration: const BoxDecoration(color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(8), topRight: Radius.circular(8))),
        child: Container(
          padding: EdgeInsets.only(top: 10.w),
          child: Stack(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      SizedBox(width: 18.w),
                      Text("预付卡", style: setTextStyle(14, FontWeight.w500, Tools.fromHex("#333333"))),
                      Text(" (剩余${widget.info['hasBdPayCardBalance']})", style: setTextStyle(14, FontWeight.w300, Tools.fromHex("#333333"))),
                    ],
                  ),
                  Container(
                    padding: EdgeInsets.fromLTRB(18.w, 10.w, 8.w, 10.w),
                    child: SingleChildScrollView(
                      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                        Container(
                            margin: EdgeInsets.only(bottom: 10.w),
                            child: Row(
                              children: [
                                BaseCheckBox(
                                  value: _selected == 1 ? true : false,
                                  onChange: (selected) {
                                    change(selected, 1);
                                  },
                                ),
                                SizedBox(width: 10.w),
                                BaseInteractive(
                                  onClick: () {
                                    change(_selected != 1 ? true : false, 1);
                                  },
                                  childWidget: Text("暂时不用预付卡抵现", style: setTextStyle(13, FontWeight.w300, Tools.fromHex("#333333"))),
                                )
                              ],
                            )),
                        Container(
                            margin: EdgeInsets.only(bottom: 10.w),
                            child: Row(
                              children: [
                                BaseCheckBox(
                                  value: _selected == 2 ? true : false,
                                  onChange: (selected) {
                                    change(selected, 2);
                                  },
                                ),
                                SizedBox(width: 10.w),
                                BaseInteractive(
                                  onClick: () {
                                    change(_selected != 2 ? true : false, 2);
                                  },
                                  childWidget: Text("最多可用抵现¥${widget.info['maxUseBdPayCardBalance']}", style: setTextStyle(13, FontWeight.w300, Tools.fromHex("#333333"))),
                                )
                              ],
                            ))
                      ]),
                    ),
                  ),
                ],
              ),
              Positioned(
                  bottom: MediaQuery.of(context).padding.bottom,
                  child: Container(
                    padding: EdgeInsets.fromLTRB(18.w, 5.w, 18.w, 5.w),
                    width: 375.w,
                    color: Colors.white,
                    child: solidButton('确定', () {
                      submit();
                    }),
                  ))
            ],
          ),
        ));
  }

  //选择
  void change(bool selected, int value) {
    setState(() {
      if (selected) {
        _selected = value;
      } else {
        _selected = 0;
      }
    });
  }

  //提交
  void submit() {
    if (_selected == 1) {
      if (widget.onConfirm != null) {
        widget.onConfirm!('0');
      }
      Get.back();
    } else {
      if (widget.onConfirm != null) {
        widget.onConfirm!(widget.info['maxUseBdPayCardBalance'].toString());
      }
      Get.back();
    }
  }
}

///唤起预付卡弹窗
void showPrepaymentCardPopup({required Map<String, dynamic> info, Function(String discountAmount)? onConfirm}) {
  showCupertinoModalPopup(
      barrierDismissible: false,
      context: Get.context!,
      builder: (context) {
        return PrepaymentCardPopup(
          info: info,
          onConfirm: onConfirm,
        );
      });
}
